package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Observable;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import edu.fim.core.exception.CaloriesThresholdExceededException;
import edu.fim.presentation.controller.ConsumerController;
import edu.fim.presentation.controller.FoodController;
import edu.fim.presentation.model.bean.Food;


public class MainForm extends AbstractForm {

	private static final long serialVersionUID = -659844447393947267L;

	private ConsumerController consumerController;
	// private FoodController foodController;
	
	private JComboBox<Food> foodComboBox;
	private JLabel caloriesIndicator;

	public MainForm(
			String title,
			ConsumerController consumerController) {
		super(title);
		this.consumerController = consumerController;
		initialize();
		update(null, null);
	}

	public void setConsumerController(ConsumerController consumerController) {
		this.consumerController = consumerController;
	}

	public void update(Observable o, Object arg) {
		updateFoodComboBox();
		Double consumedCalories = consumerController.getCurrentConsumer().getCaloriasConsumidas();
		caloriesIndicator.setText(consumedCalories.toString());
		pack();
	}
	
	private void updateFoodComboBox() {
		foodComboBox.removeAllItems();
		for (Food food : consumerController.getCurrentConsumer().getMenu().getFoods()) {
			foodComboBox.addItem(food);
		}
	}

	protected void initializeDynamicComponents() {
		initializeFoodComboBox();
		caloriesIndicator = new JLabel();
	}

	private void initializeFoodComboBox() {
		foodComboBox = new JComboBox<Food>();
		foodComboBox.setMinimumSize(new Dimension(150, 20));
		foodComboBox.setPreferredSize(new Dimension(150, 20));
		foodComboBox.setMaximumSize(new Dimension(150, 20));
		updateFoodComboBox();
	}

	protected void buildLayout() {
		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		Container container = new Container();
		container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

		getContentPane().add(foodComboBox);
		getContentPane().add(criarComponenteRegistroConsumo());
		getContentPane().add(new JLabel("Total Calories Consumed: "));
		getContentPane().add(caloriesIndicator);
		getContentPane().add(criarComponenteAcessoPerfil());
		getContentPane().add(createMenuConfigAccessComponent());
		getContentPane().add(criarComponenteEncerramento());
	}
	
	private Component criarComponenteRegistroConsumo() {
		AbstractButton componenteRegistroConsumo = new JButton("Register Ingestion");
		componenteRegistroConsumo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteRegistroConsumoActionPerformed();
			}
		});
		return componenteRegistroConsumo;
	}
	
	private Component criarComponenteAcessoPerfil() {
		AbstractButton componenteAcessoPerfil = new JButton("Profile");
		componenteAcessoPerfil.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteAcessoPerfilActionPerformed(evt);
			}
		});
		return componenteAcessoPerfil;
	}
	
	private Component createMenuConfigAccessComponent() {
		AbstractButton button = new JButton("Menu Config");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				menuConfigAccessComponentActionPerformed();
			}
		});
		return button;
	}
	
	private Component criarComponenteEncerramento() {
		AbstractButton componenteEncerramento = new JButton("Exit");
		componenteEncerramento.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteEncerramentoActionPerformed();
			}
		});
		return componenteEncerramento;
	}

	private void componenteRegistroConsumoActionPerformed() {
		Food food = (Food) foodComboBox.getSelectedItem();
		try {
			consumerController.registerIngestion(food);
		}
		catch (CaloriesThresholdExceededException e) {
			showMessage(e.getMessage());
			e.printStackTrace();
		}
		catch (SQLException e) {
			showMessage("There was an error while registered the consumption.");
			e.printStackTrace();
		}
	}
	
	private void componenteAcessoPerfilActionPerformed(ActionEvent evt) {
		Form telaPerfil = new ProfileForm("Perfil", consumerController);
		consumerController.getCurrentConsumer().addObserver(telaPerfil);
		telaPerfil.setVisible(true);
	}
	
	private void menuConfigAccessComponentActionPerformed() {
		FoodController foodController = new FoodController();
		Form menuConfigForm = new MenuConfigForm("Menu Config", this, consumerController, foodController);
		menuConfigForm.setVisible(true);
	}
	
	private void componenteEncerramentoActionPerformed() {
		setVisible(false);
	}
}